package org.project.s.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Fengye;
import org.project.pojo.WxUser;
import org.project.service.user.WxUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Controller
@RequestMapping("/wxuser")
public class Wxusercontroller {

	@Resource
	private WxUserService wxUserService;
	
	@RequestMapping("/selectallwxuser")
	public String selectallwxuser(HttpServletRequest request, HttpServletResponse response){
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		
		String str=request.getParameter("n");   
//		System.out.println(str);
		Fengye fengye=new Fengye();
	     //总行数  
	     int lineCount=wxUserService.selectwxusercount();
	     //每页的行数  
	     int pageSize=10;  
	     //总共的页数  
	     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
	       
	     int n = 0;  
	               
	     if(str == null || str.equals("")){    //多次点击首页
	         n = 0;  
	     }else if(str.equals("a")){  //上一页
	    	 
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n-pageSize;  
	     }else if(str.equals("b")){          //下一页
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n+pageSize;  
	     }else if(str.equals("0")){  
	         n=0;  
	     }else{                           //点击 尾页
	         n=(pageCount-1)*pageSize;  
	     }     
	       
	     if(n>((pageCount-1)*pageSize)){    //多次点击尾页
	         n=(pageCount-1)*pageSize;  
	     }  
	     if(n<0){  
	         n=0;  
	     }     
	       
	       
	       
	     request.getSession().setAttribute("pageNow", n);  
	     request.getSession().setAttribute("pageCount", pageCount);    
	       
	   
	   /*  System.out.println(n);
	     System.out.println(pageSize);*/
	     fengye.setN(n);
	     fengye.setPageSize(pageSize);
	     List<WxUser> wxuserlist= wxUserService.selectallwxuser(fengye);
	    
		       req.setAttribute("wxuserlist", wxuserlist);
		       req.setAttribute("wxusercount",wxUserService.selectwxusercount() );
	          
		   
		return "showwxuser";
	}
	
	
	
	@RequestMapping("/deletewxuser")
	public String deletewxuser(@Param("openid")String openid){
		
	  int i=wxUserService.deletewxuser(openid);
		if(i>0){
			return "mainfra";
		}else{
			
			return "mainfra";
		}
	}
}
